package com.shanyu.wechat.util;

import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.corpus.tag.Nature;
import com.hankcs.hanlp.seg.Segment;
import com.hankcs.hanlp.seg.common.Term;
import com.shanyu.wechat.entity.JianDaoYunEntity;
import com.shanyu.wechat.entity.ProjectEntity;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * 摘要词工具（for nlp）
 */
public class SummaryWordsExtractor {


    private static final Integer KEYWORD_NUM = 10;

    /**
     * 摘要中抽取关键词
     *
     * @param keyword
     */
    public static List<String> extractKeyword(String keyword) {
        return HanLP.extractKeyword(keyword, KEYWORD_NUM);
    }

    public static ProjectEntity parse(String string, String shouDao) {
        List<Term> list = HanLP.segment(string);
        ProjectEntity projectEntity = new ProjectEntity().setOriginMsg(string).setBusinessPeople(shouDao);
        StringBuilder stringBuilder = new StringBuilder();
        for (Term term : list) {
            if (StringUtils.equals(term.nature.toString(), "nr")) {
                projectEntity.setContactPerson(term.word);
            }
            if (StringUtils.equals(term.nature.toString(), "m")) {
                projectEntity.setPhone(term.word);
            }
            if (StringUtils.equals(term.nature.toString(), "ns") || StringUtils.equals(term.nature.toString(), "p") || StringUtils.equals(term.nature.toString(), "n") || StringUtils.equals(term.nature.toString(), "v")) {
                stringBuilder.append(term.word);
            }
        }
        projectEntity.setProjectProduct(stringBuilder.toString());
        return projectEntity;
    }

    public static JianDaoYunEntity parseJianDaoYun(String str){
        Segment segment = HanLP.newSegment().enableAllNamedEntityRecognize(true);
//                .enableNameRecognize(true)
//                .enablePlaceRecognize(true)
//                .enableOrganizationRecognize(true);

        List<Term> list = segment.seg(str);
        String phone = "";
        String area = "";
        String man = "";

        for (Term term : list) {
            //联系人电话
            if (term.nature == Nature.m && term.word.length() == 11) {
                phone += term.word;
            }
            //联系人称呼
            if (term.nature == Nature.nr) {
                man += term.word;
            }
            //要货区域
            if (term.nature == Nature.ns) {
                area += term.word;
            }
        }
        return new JianDaoYunEntity().setPhone(phone).setArea(area).setMan(man);
    }


}
